package com.example.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;

import com.example.gulimallproduct.request.SaveCategoryRequest;
import com.example.gulimallproduct.response.SelectCategoryListWithTreeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimallproduct.entity.CategoryEntity;
import com.example.gulimallproduct.service.CategoryService;
import com.example.common.utils.R;



/**
 * 商品三级分类
 *
 * @author zzl
 * @email zzl@gmail.com
 * @date 2025-10-24 15:22:38
 */
@RestController
@RequestMapping("gulimallproduct/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类树形列表
     */
    @GetMapping("/list")
    public R SelectCategoryListWithTree(){
        log.info("获取分类树形列表");
        List<SelectCategoryListWithTreeResponse> categoryList = categoryService.SelectCategoryListWithTree();
        return R.ok().put("data", categoryList);
    }


    /**
     * 信息
     */
    @GetMapping("/getInfo/{catId}")
    public R info(@PathVariable("catId") Long catId){
        log.info("获取分类信息:{}", catId);
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/saveCategory")
    public R save(@RequestBody SaveCategoryRequest saveCategoryRequest){
        log.info("保存分类:{}", saveCategoryRequest);
        return categoryService.saveCategory(saveCategoryRequest);
    }

    /**
     * 修改
     */
    @PostMapping("/updateCategory")
    public R update(@RequestBody CategoryEntity category){
        log.info("修改分类:{}", category);

        return categoryService.updateCategory(category);
    }

    /**
     * 删除
     */
    @PostMapping("/delCategoryByIds")
    public R delete(@RequestBody Long[] catIds){
        log.info("删除分类:{}", Arrays.toString(catIds));
        return categoryService.delCategoryByIds(catIds);
    }

}
